package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.user.UserConstans;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.behavior.dto.FollowBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.utils.threadlocal.ApUserThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    @Autowired
    ArticleFeign articleFeign;

    @Autowired
    ApUserFanService apUserFanService;

    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * 关注取消关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //检查参数
        if (dto==null||dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询作者
        ApAuthor author = articleFeign.findByAputhorId(dto.getAuthorId());
        if (author==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
       //判断用户是否登录
        ApUser user = ApUserThreadLocalUtils.getUser();
        if (user==null||user.getId()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApUserFollow userFollow = getOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, author.getUserId()).eq(ApUserFollow::getUserId, user.getId()));
        ApUserFan userFan = apUserFanService.getOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getUserId, author.getUserId()).eq(ApUserFan::getFansId, user.getId()));

        if (dto.getOperation().shortValue()==UserConstans.TYPE_OPERATION_YES){ //判断是关注还是取消关注
            if (userFollow==null){
               userFollow=new ApUserFollow();
               userFollow.setFollowId(author.getUserId());
               userFollow.setUserId(user.getId());
               userFollow.setCreatedTime(new Date());
               userFollow.setFollowName(author.getName());
               userFollow.setLevel((short)1);
               userFollow.setIsNotice(true);
           }
           //添加关注
           saveOrUpdate(userFollow);
            //通过kafka发送异步消息，保存行为
            FollowBehaviorDto followBehaviorDto=new FollowBehaviorDto();
            followBehaviorDto.setUserId(user.getId());
            followBehaviorDto.setArticleId(dto.getArticleId());
            followBehaviorDto.setFollowId(author.getUserId());
            kafkaTemplate.send("followBehavior", JSON.toJSONString(followBehaviorDto));

            //添加粉丝
            if (userFan==null){
                userFan.setCreatedTime(new Date());
                userFan.setFansId(user.getId().longValue());
                userFan.setFansName(user.getName());
                userFan.setIsDisplay(true);
                userFan.setIsShieldComment(false);
                userFan.setIsShieldLetter(true);
                userFan.setLevel((short)0);
                userFan.setUserId(author.getUserId());
            }
            apUserFanService.saveOrUpdate(userFan);
       }else if (dto.getOperation().shortValue()==UserConstans.TYPE_OPERATION_NO){
            if (userFollow!=null){
                removeById(userFollow.getId());
            }
            if (userFan!=null){
                apUserFanService.removeById(userFan.getId());
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
